package com.atguigui.leetcode;

/**
 * 565.数组嵌套
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2022/7/17 21:05
 */
public class P565ArrayNesting {
    public static void main(String[] args) {
        Solution solution = new P565ArrayNesting().new Solution();
        // TO TEST
    }

    class Solution {

        /**
         * 图解法
         *
         * @param nums
         * @return
         */
        public int arrayNesting(int[] nums) {
            int ans = 0, n = nums.length;
            //标记是否已经被用过
            boolean[] vis = new boolean[n];
            for (int i = 0; i < n; i++) {
                int cut = 0;
                while (!vis[i]) {
                    vis[i] = true;
                    i = nums[i];
                    cut++;
                }

                ans = Math.max(ans, cut);
            }

            return ans;
        }
    }
}
